package com.sctvcloud.zhaohua_sd.ui.adapter.holder;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.sctvcloud.zhaohua_sd.R;
import com.sctvcloud.zhaohua_sd.beans.TopicContent;
import com.sctvcloud.zhaohua_sd.beans.TopicListItem;
import com.sctvcloud.zhaohua_sd.ui.util.IListShowData;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.xutils.common.util.LogUtil;

import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TopicHolder extends BaseRebllionHolder {

    @BindView(R.id.id_flowlayout)
    protected TagFlowLayout flowlayout;
    TagAdapter<TopicContent> tagAdapter;

    private LayoutInflater mInflater;
    private OnTopicSelectListener listener;
    private Integer integer =0;
    public TopicHolder(Context context, View itemView,OnTopicSelectListener listener ) {
        super(context, itemView);
        mInflater = LayoutInflater.from(context);
        this.listener = listener;
    }

    @Override
    public void onDestory() {
        super.onDestory();
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(IListShowData iListShowData, boolean isEditing) {
        super.setData(iListShowData, isEditing);
        if (iListShowData instanceof TopicListItem) {
            TopicListItem item = (TopicListItem) iListShowData;



            tagAdapter = new TagAdapter<TopicContent>(item.getTopicList()) {

                @Override
                public View getView(com.zhy.view.flowlayout.FlowLayout parent, int position, TopicContent s) {
                    TextView tv = (TextView) mInflater.inflate(R.layout.flow_text,
                            flowlayout, false);
                    tv.setText(s.getTopicContent());
                    return tv;
                }

                @Override
                public boolean setSelected(int position, TopicContent s) {
                    return false;
                }
            };

            flowlayout.setAdapter(tagAdapter);
            flowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                @Override
                public boolean onTagClick(View view, int position, com.zhy.view.flowlayout.FlowLayout parent) {
                    return true;
                }
            });

            flowlayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
                @Override
                public void onSelected(Set<Integer> selectPosSet) {
                    if (null != selectPosSet && selectPosSet.size() > 0) {
                        for (Integer i : selectPosSet) {
//                            tp = tagAdapter.getItem(i);
                            if(null!=listener){
                                integer = i;
                                listener.OnTopicSelected(i,tagAdapter.getItem(i));
                            }
                            LogUtil.e("------TopicHolder-------onSelected --"+tagAdapter.getItem(i));
                        }
                    }
                    //else {
//                        tp = null;
//                    }

                }

            });
            tagAdapter.setSelectedList(integer);
        }
    }

    public interface OnTopicSelectListener{
        void OnTopicSelected(Integer selectInteger,TopicContent tp);
    }
}
